<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>忘记密码页面</title>
</head>
<body>
        <div>请输入用户名或手机号：<input type="text" name="studentName" id="studentName">
            <button id="queryBtn">提交</button>
        </div>

        <div id="securityQuestion" style="display: none">
            <div>
                密保问题：<select name="question" id="question"></select>
            </div>

            <div>答案： <input type="text" name="answer" id="answer"></div>
            <div><button id="answerBtn">提交</button></div>
        </div>
</body>
</html>
<script src="http://code.jquery.com/jquery-2.1.1.min.js"></script>
<script>
    $(document).ready(function (){
        var stuId;

        $("#queryBtn").click(function (){
            var data = {'stuNameOrPhoneNo':$("#studentName").val()};
            $.ajax({
                method:"post",
                url:"student?action=querySecurityQuestion",
                data:data,
                type:"json",
                success:function (result){
                    $("#securityQuestion").css("display",'');

                    //取出后端发送过来的问题集合
                    var questions = result.questions;
                    console.log(questions);

                    //取出前端发送的用户id，在提示密保问题答案时使用
                    stuId = result.stuId;
                    var option = "";
                    for(var i = 0; i < questions.length; i++){
                        option = option +'<option value="' + questions[i] +'">'+ questions[i] +'</option>'
                    }
                    $("#question").html(option);
                }
            });
        })


        $("#answerBtn").click(function (){
            var data = {"stuId":stuId,
                "question": $("#question option:selected").text(),
                "answer":$("#answer").val()};
            $.ajax({
                method:"post",
                url:"student?action=checkSecurityQuestion",
                data:data,
                type:"json",
                success:function (result){
                   //如果验证成功，则提示密码问题回答正确，并跳转到修改密码的界面，否则让其重新输入
                    if ( result.code == 30001){
                        alert(result.msg);
                        location.href="forgerUpdatePassword.html";
                    }else {
                        alert(result.msg);
                    }
                }
            });
        })

    })


</script>